//////////////////////////////////////////////////////////////////////////////////
//	File	:	SmokeParticleSystem.cpp
//	Author	:	Yuri Oyoko
//	Purpose	:	The smoke particle system defines the behaviour of the in-game
//				smoke particle effects. The definitions of the in-game particle
//				effects are contained in the SmokeSystem.bin file created by
//				the particle editor
//////////////////////////////////////////////////////////////////////////////////

#include "SmokeParticleSystem.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "Definitions.h"

#include <iostream>
#include <fstream>
using namespace std;

struct InfoReader
{
	char TextureName[128];
	float MinParticleSpeed;
	float MaxParticleSpeed;

	float MinParticleAcceleration;
	float MaxParticleAcceleration;

	float MinParticleLifetime;
	float MaxParticleLifetime;

	float MinParticleScale;
	float MaxParticleScale;

	int MinNumberOfParticles;
	int MaxNumberOfParticles;

	float MinParticleRotationSpeed;
	float MaxParticleRotationSpeed;

	int ParticleColorR;
	int ParticleColorG;
	int ParticleColorB;

	float MinimumParticleAngle;
	float MaximumParticleAngle;

	float EmitterSpawnRate;
};
SmokeParticleSystem::SmokeParticleSystem(int EffectsCount)
{
	NumberOfEffects = EffectsCount;
}
SmokeParticleSystem::~SmokeParticleSystem()
{
	
}
void SmokeParticleSystem::LoadParticleSystem()
{
	ifstream fileReader(SmokeParticleSystemFile, std::ios_base::in | std::ios_base::binary);
	
	InfoReader t;
	int strlength = 0;
	//Texture name
	fileReader.read((char*)&strlength, sizeof(int));
	fileReader.read(t.TextureName, strlength);
	
	double temp = 0.0;
	//Particle speed
	fileReader.read((char*)&temp, sizeof(double));
	t.MinParticleSpeed = (float)temp;
	fileReader.read((char*)&temp, sizeof(double));
	t.MaxParticleSpeed = (float)temp;

	//Particle Acceleration
	fileReader.read((char*)&temp, sizeof(double));
	t.MinParticleAcceleration = (float)temp;
	fileReader.read((char*)&temp, sizeof(double));
	t.MaxParticleAcceleration = (float)temp;
	

	//Particle lifetime
	fileReader.read((char*)&temp, sizeof(double));
	t.MinParticleLifetime = (float)temp;
	fileReader.read((char*)&temp, sizeof(double));
	t.MaxParticleLifetime = (float)temp;
	
	//Particle scale
	fileReader.read((char*)&temp, sizeof(double));
	t.MinParticleScale = (float)temp;
	fileReader.read((char*)&temp, sizeof(double));
	t.MaxParticleScale = (float)temp;

	//Particle count
	fileReader.read((char*)&t.MinNumberOfParticles, sizeof(int));
	fileReader.read((char*)&t.MaxNumberOfParticles, sizeof(int));

	//Rotation Speed
	fileReader.read((char*)&temp, sizeof(double));
	t.MinParticleRotationSpeed = (float)temp;
	fileReader.read((char*)&temp, sizeof(double));
	t.MaxParticleRotationSpeed = (float)temp;
	
	//Color
	fileReader.read((char*)&t.ParticleColorR, sizeof(int));
	fileReader.read((char*)&t.ParticleColorG, sizeof(int));
	fileReader.read((char*)&t.ParticleColorB, sizeof(int));

	//Angles
	fileReader.read((char*)&temp, sizeof(double));
	t.MinimumParticleAngle = (float)temp;
	fileReader.read((char*)&temp, sizeof(double));
	t.MaximumParticleAngle = (float)temp;

	//Emitter spawn rate
	fileReader.read((char*)&temp, sizeof(double));
	t.EmitterSpawnRate = (float)temp;

	fileReader.close();


	ParticleTexture = CSGD_TextureManager::GetInstance()->LoadTexture(t.TextureName);
	MinParticleSpeed = t.MinParticleSpeed;
	MaxParticleSpeed = t.MaxParticleSpeed;

	MinParticleAngle = t.MinimumParticleAngle;
	MaxParticleAngle = t.MaximumParticleAngle;
	
	MinParticleAcceleration = t.MinParticleAcceleration;
	MaxParticleAcceleration = t.MaxParticleAcceleration;

	MinParticleRotationSpeed = t.MinParticleRotationSpeed;
	MaxParticleRotationSpeed = t.MaxParticleRotationSpeed;

	MinParticleLifetime = t.MinParticleLifetime;
	MaxParticleLifetime = t.MaxParticleLifetime;

	MinParticleCount = t.MinNumberOfParticles;
	MaxParticleCount = t.MaxNumberOfParticles;

}
void SmokeParticleSystem::InitializeConstants()
{
	LoadParticleSystem();
}

void SmokeParticleSystem::Initialize()
{
	BaseParticleSystem::Initialize();
}